package command.pattern;

/**
 * Created by DELL
 * Date: 2019/10/9.
 * Time: 8:25
 */
interface ICommand{
    void Do();
}

class Receiver{
    public void performUndo(){
        System.out.println("执行 -MyUndoComamnd");
    }
    public void performRedo(){
        System.out.println("执行 -MyRedoComamnd");
    }
}
class MyUndoCommand implements ICommand{
    private Receiver receiver;

    public MyUndoCommand(Receiver receiver) {
        this.receiver = receiver;
    }

    @Override
    public void Do() {
        receiver.performUndo();
    }
}

class MyRedoCommand implements ICommand{
    private Receiver receiver;

    public MyRedoCommand(Receiver receiver) {
        this.receiver = receiver;
    }

    @Override
    public void Do() {
        receiver.performRedo();
    }
}

class Invoke{
    ICommand cmd;
    public void executeComamnd(ICommand cmd){
        this.cmd = cmd;
        cmd.Do();
    }
}
public class CommandPatternEx {
    public static void main(String[] args) {
        System.out.println("***Command Pattern Demo***");
        Receiver intenddeReceiver = new Receiver();
        /*Client holds Invoker and Command Objects*/
        Invoke inv = new Invoke();
        MyUndoCommand unCmd = new MyUndoCommand(intenddeReceiver);
        MyRedoCommand reCmd = new MyRedoCommand(intenddeReceiver);

        inv.executeComamnd(unCmd);
        inv.executeComamnd(reCmd);


    }
}
